package com.topisv.tms.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.PackformStyle;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.PackformStyleService;
import com.topisv.tms.utils.SystemUtil;

@Controller
@RequestMapping(value = "v2/packformStyle")
public class PackformStyleController {
	@Autowired
	private PackformStyleService packformStyleService;


	@Autowired
	private CustomersService customersService;

	@RequestMapping(value = "")
	public String init(Model model) {
		List<Customers> customers = customersService.findAll();
		model.addAttribute("customers", customers);

		return "packformStyle/styleList";
	}
	
	@RequestMapping(value = "DeletepackStyle/{id}")
	public String DeletepackStyle(@PathVariable("id") Long id,Model model) {
		packformStyleService.delete(id);
		List<Customers> customers = customersService.findAll();
		model.addAttribute("customers", customers);

		return "packformStyle/styleList";
	}
	
	@RequestMapping(value = "deletepackStyleNew/{id}",method = RequestMethod.POST)
	public void deletepackStyleNew(@PathVariable("id") Long id,
			@RequestParam(value="customerCode") String customerCode,
			HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			packformStyleService.delete(id);
			List<PackformStyle> packformStyles=packformStyleService.findByCustomerCode(customerCode);
			if(packformStyles==null||packformStyles.isEmpty()){
				json.put("status", "-1");
				json.put("content", customerCode);
			}else{
				json.put("status", "1");
				json.put("content", packformStyles);
			}
			
			out.print(json.toString());
		} catch (IOException e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	@RequestMapping(value = "add/{customerCode}")
	public String addPackformStyle(@PathVariable("customerCode") String customerCode, Model model) {
		Customers customers=customersService.findByCustomerCode(customerCode);
		model.addAttribute("titlePage", "增加随箱单");
		model.addAttribute("customers", customers);
		return "packformStyle/addStyle";
	}
	
	
	@RequestMapping(value = "ajaxCustomerCodePackformStyle")
	public void ajaxCustomerCodePackformStyle(
			@RequestParam(value="customerCode") String customerCode,
			HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			List<PackformStyle> packformStyles=packformStyleService.findByCustomerCode(customerCode);
			if(packformStyles==null||packformStyles.isEmpty()){
				json.put("status", "-1");
				json.put("content", customerCode);
			}else{
				json.put("status", "1");
				json.put("content", packformStyles);
			}
			
			out.print(json.toString());
		} catch (IOException e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}


	/**
	 * 增加随箱单样式
	 * @param content
	 * @param brandId
	 * @param styleName
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "addAjaxPackformStyle")
	public void addAjaxPackformStyle(
			@RequestParam(value="content") String content,
			@RequestParam(value="customerCode") String customerCode,
			@RequestParam(value="styleName") String styleName,
			@RequestParam(value="type") String type,
			HttpServletRequest request,
			HttpServletResponse response) {
		PackformStyle packformStyle = new PackformStyle();
		PrintWriter out = null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			//content=URLDecoder.decode(content, "utf-8");
			packformStyle.setCustomerCode(customerCode);
			packformStyle.setStyleContent(content);
			packformStyle.setStyleName(styleName);
			packformStyle.setType(type);
			packformStyleService.savePackformStyle(packformStyle);
			json.put("status", "1");
			json.put("content", "创建成功");
			out.print(json.toString());
		} catch (IOException e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	/**
	 * 获得随箱单样式
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "getAjaxPackformStyle")
	public void getAjaxPackformStyle(
			@RequestParam(value="packformStylesId") Long packformStylesId,
			HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			out = response.getWriter();
			
			PackformStyle packformStyle = packformStyleService.findOne(packformStylesId);
			if(packformStyle==null){
				json.put("status", "-1");
				json.put("content", "该样式不存在");
				out.print(json.toString());
				return;
			}
			json.put("status", "1");
			json.put("content", packformStyle.getStyleContent());
			json.put("name", packformStyle.getStyleName());
			json.put("type", packformStyle.getType());
			out.print(json.toString());
		} catch (IOException e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	@RequestMapping(value = "selectList/{id}")
	public String selectList(@PathVariable("id") Long id, Model model,RedirectAttributes redirectAttributes) {
		
		List<Customers> customers = customersService.findAll();
		model.addAttribute("customers", customers);
		return "packformStyle/styleList";
	}


	/**
	 * 更新随箱单样式
	 * @param packformStyleId
	 * @param content
	 * @param styleName
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "updateAjaxPackformStyle")
	public void updateAjaxPackformStyle(
			@RequestParam(value="packformStyleId") Long packformStyleId,
			@RequestParam(value="content") String content,
			@RequestParam(value="styleName") String styleName,
			@RequestParam(value="type") String type,
			HttpServletRequest request,
			HttpServletResponse response) {
		PrintWriter out = null;
		JSONObject json=null;
		try {
			out = response.getWriter();
			json=new JSONObject();
			PackformStyle packformStyle = packformStyleService.findOne(packformStyleId);
			if (packformStyle==null) {
				json.put("status", "-1");
				json.put("content", "还没有样式");
				out.print(json.toString());
				return;
			}
			//content=URLDecoder.decode(content, "utf-8");
			packformStyle.setStyleContent(content);
			packformStyle.setStyleName(styleName);
			packformStyle.setType(type);
			packformStyleService.savePackformStyle(packformStyle);
			json.put("status", "1");
			json.put("content", "修改成功");
			out.print(json.toString());
		} catch (IOException e) {
			json.put("status", "0");
			json.put("content", e.getMessage());
			out.print(json.toString());
			
		}
	}

	/**
	 * 修改随箱单
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "updatePackStyle/{id}")
	public String updatePackStyle(@PathVariable("id") Long id, Model model,
			HttpServletRequest request) {
		PackformStyle packformStyle = packformStyleService.findOne(id);
		String path = request.getRealPath("");
		try {
			getContent(path, packformStyle);
			model.addAttribute("packformStyle", packformStyle);
			model.addAttribute("titlePage", "修改快递单");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			
		}
		return "packformStyle/updateStyle";
		//return "packformStyle/updatePackformStyle";
	}

	private PackformStyle getContent(String path, PackformStyle packformStyle)
			throws IOException {
		String content = packformStyle.getStyleContent();
		packformStyle.setStyleContent(content.replace(SystemUtil.getLineSeparator(), ""));
		return packformStyle;
	}
}
